﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography
;

namespace Forget.HRBLL
{
    public class Common
    {
        public static string GetMD5(string content)
        {
            if (string.IsNullOrEmpty(content))
                throw new ArgumentException("content must be exist.");
            
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(content));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString().ToUpper();
        }

        public static string GetRandomChars(int size)
        {
            Random charRand = new Random();
            StringBuilder sBuilder = new StringBuilder(size);
            for (int i = 0; i < size; i++)
            {
                int tmp = charRand.Next(0, 3);
                switch (tmp)
                {
                    case 1: //char
                        sBuilder.Append(Common.GetRandChar(i));
                        break;
                    case 2: //number
                        sBuilder.Append(charRand.Next(-1, 10));
                        break;
                }
            }
            return sBuilder.ToString();
        }

        private static string GetRandChar(int index)
        {
            Random rand = new Random();
            int tmp = rand.Next(0, 27);
            tmp+=96;


            if ((index & 1) == 0)
                return ((char)tmp).ToString().ToUpper();
            else
                return ((char)tmp).ToString().ToLower();
        }               
    }
}
